import java.text.NumberFormat;
import java.util.*;
public class NumberCount {
    public static void main(String[] args) {
        NumberFormat nf = NumberFormat.getPercentInstance();
        IntNumber in1, in2;
        Score score1, score2, num2;
        int num, N;
        String Q, num1,S1,S2;
        Random ran = new Random();
        Scanner scan = new Scanner(System.in);
        IOFile iof = new IOFile("Exercises.txt");
        while (true) {
            System.out.print("请输入您所需要的题目数量：");
            int A = scan.nextInt();
            for (int i = 0; i < A; i++) {
                int B = ran.nextInt(2);
                int C = ran.nextInt(4);
                in1 = IntNumber.obj();
                in2 = IntNumber.obj();
                score1 = Score.obj();
                score2 = Score.obj();
                if (B == 0) {
                    switch (C) {
                        case 0:
                            num = in1.add(in2);
                            S1 =in1.toString()+ " + " + in2.toString() + "=";
                            num1 = "" + num;
                            iof.WriteQuestionsToFile(S1);
                            N = scan.nextInt();
                            CorrectJudgment.judgment(N == num, num1);
                            break;
                        case 1:
                            num = in1.subtract(in2);
                            S1 =in1.toString() + " - " + in2.toString() + "=";
                            num1 = "" + num;
                            iof.WriteQuestionsToFile(S1);
                            N = scan.nextInt();
                            CorrectJudgment.judgment(N == num, num1);
                            break;
                        case 2:
                            num = in1.multiply(in2);
                            S1=in1.toString() + " * " + in2.toString() + "=";
                            num1 = "" + num;
                            iof.WriteQuestionsToFile(S1);
                            N = scan.nextInt();
                            CorrectJudgment.judgment(N == num, num1);
                            break;
                        case 3:
                            num1 = in1.divide(score1);
                            S1 =in1.toString() + " / " + score1.toString() + "=";
                            Q = scan.next();
                            CorrectJudgment.judgment(Q.equals(num1), num1);
                            iof.WriteQuestionsToFile(S1);
                            break;
                    }
                } else {
                    switch (C) {
                        case 0:
                            num2 = score1.add(score2);
                            num1 = num2.toString();
                            Q = scan.next();
                            S2 =score1.toString()+"+"+score2.toString()+"=";
                            iof.WriteQuestionsToFile(S2);
                            CorrectJudgment.judgment(Q.equals(num1), num1);
                            break;
                        case 1:
                            num2 = score1.subtract(score2);
                            num1 = num2.toString();
                            Q = scan.next();
                            S2 =score1.toString()+"-"+score2.toString()+"=";
                            iof.WriteQuestionsToFile(S2);
                            CorrectJudgment.judgment(Q.equals(num1), num1);
                            break;
                        case 2:
                            num2 = score1.multiply(score2);
                            num1 = num2.toString();
                            Q = scan.next();
                            S2 =score1.toString()+"*"+score2.toString()+"=";
                            iof.WriteQuestionsToFile(S2);
                            CorrectJudgment.judgment(Q.equals(num1), num1);
                            break;
                        case 3:
                            num2 = score1.divide(score2);
                            num1 = num2.toString();
                            Q = scan.next();
                            S2 =score1.toString()+"÷"+score2.toString()+"=";
                            iof.WriteQuestionsToFile(S2);
                            CorrectJudgment.judgment(Q.equals(num1), num1);
                            break;
                    }
                }
            }
            System.out.println("你答对的题目总数：" + CorrectJudgment.getTrues());
            double T = (double) CorrectJudgment.getTrues() / A;
            System.out.println("您的正确率为:" + nf.format(T));
            System.out.println("是否继续生成题目？（y/n）:"  );
            String s1 = scan.next();
            if (s1.equalsIgnoreCase("n")){
                break;
            }
        }
    }
}
